package com.beichuan_travel.service.impl;

import com.beichuan_travel.enums.ResultCode;
import com.beichuan_travel.enums.Sort;
import com.beichuan_travel.exception.ApiException;
import com.beichuan_travel.interfaces.transform.AttractionCovertBasic;
import com.beichuan_travel.mapper.AttractionMapper;
import com.beichuan_travel.model.dto.AttractionDTO;
import com.beichuan_travel.model.po.AttractionPO;
import com.beichuan_travel.model.vo.AttractionVO;
import com.beichuan_travel.model.vo.ResultVO;
import com.beichuan_travel.service.AttractionService;
import com.beichuan_travel.utils.ArraysUtils;
import com.beichuan_travel.utils.ResponseResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("attractionService")
public class AttractionServiceImpl implements AttractionService {

    @Resource
    AttractionMapper attractionMapper;

    @Resource
    ResponseResult  responseResult;

    @Resource
    ArraysUtils arraysUtils;


    @Override
    public ResultVO queryByName(String name) {
        AttractionPO attractionPO = attractionMapper.queryByName(name);
        AttractionVO attractionVO = new AttractionVO();
        attractionVO.setImgUrl(attractionMapper.imgUrl(attractionPO.getId()));
        attractionVO.setId(attractionPO.getId());
        attractionVO.setName(attractionPO.getName());
        attractionVO.setLocation(attractionPO.getLocation());
        attractionVO.setStar(attractionPO.getStar());
        attractionVO.setPrice(attractionPO.getPrice());
        attractionVO.setDescription(attractionPO.getDescription());

        return attractionVO != null ? responseResult.ok(attractionVO) : responseResult.empty();
    }

    @Override
    public List<AttractionVO> queryByPrice(Integer lower, Integer upper) {
        List<AttractionPO> list = attractionMapper.queryByPrice(lower, upper);
        if (list == null && list.isEmpty()) {
            return null;
        }
        List<AttractionVO> attractionVOList = AttractionCovertBasic.INSTANCE.POtoCoertVO(list);
        return attractionVOList;
    }

    @Override
    public List<AttractionVO> queryByStar(Integer star) {
        List<AttractionPO> list = attractionMapper.queryByStar(star, star + 1);
        if (list == null && list.isEmpty()) {
            return null;
        }
        List<AttractionVO> attractionVOList = AttractionCovertBasic.INSTANCE.POtoCoertVO(list);
        return attractionVOList;
    }

    @Override
    public List<AttractionVO> queryAllDescByStar(Integer type) {
        List<AttractionPO> list = attractionMapper.queryAllByStarDesc();
        if (list == null && list.isEmpty()) {
            return null;
        }
        if (type != Sort.ASC.getSort() && type != Sort.DESC.getSort()) {
            throw new ApiException(ResultCode.VALIDATE_FAILED, "请传入数字0或数字1");
        }
        if (type == Sort.ASC.getSort()) {
            arraysUtils.reverse(list);
        }
        List<AttractionVO> attractionVOList = AttractionCovertBasic.INSTANCE.POtoCoertVO(list);
        return attractionVOList;
    }

    @Override
    public List<AttractionVO> queryAllASCByPrice(Integer type) {
        List<AttractionPO> list = attractionMapper.queryAllByPriceAsc();
        if (list == null && list.isEmpty()) {
            return null;
        }
        if (type != Sort.ASC.getSort() && type != Sort.DESC.getSort()) {
            throw new ApiException(ResultCode.VALIDATE_FAILED, "请传入数字0或数字1");
        }
        if (type == Sort.DESC.getSort()) {
            arraysUtils.reverse(list);
        }
        List<AttractionVO> attractionVOList = AttractionCovertBasic.INSTANCE.POtoCoertVO(list);
        return attractionVOList;
    }

    @Override
    public List<AttractionVO> fuzzyQueryName(String name) {
        List<AttractionPO> attractionPOList = attractionMapper.queryByLikeName("%"+name+"%");
        if (attractionPOList == null && attractionPOList.isEmpty()) {
            return null;
        }
        List<AttractionVO> attractionVOList = AttractionCovertBasic.INSTANCE.POtoCoertVO(attractionPOList);
        return attractionVOList;
    }

    @Override
    public List<AttractionVO> queryAllByName(Integer type) {
        List<AttractionPO> attractionPOList = attractionMapper.queryAllByNameAsc();
        if (attractionPOList == null && attractionPOList.isEmpty()) {
            return null;
        }
        if (type != Sort.ASC.getSort() && type != Sort.DESC.getSort()) {
            throw new ApiException(ResultCode.VALIDATE_FAILED, "请传入数字0或数字1");
        }
        if (type == Sort.DESC.getSort()) {
            arraysUtils.reverse(attractionPOList);
        }
        List<AttractionVO> attractionVOList = AttractionCovertBasic.INSTANCE.POtoCoertVO(attractionPOList);
        return attractionVOList;
    }

    @Override
    public int addAttraction(AttractionDTO attractionDTO) {
        AttractionPO attractionPO = new AttractionPO();
        attractionPO.setDescription(attractionDTO.getDescription());
        attractionPO.setLocation(attractionDTO.getLocation());
        attractionPO.setName(attractionDTO.getName());
        attractionPO.setPrice(attractionDTO.getPrice());
        attractionPO.setStar(attractionDTO.getStar());
        return attractionMapper.addAttraction(attractionPO);
    }
}
